package com.ruoyi.lvlian.controller;

import com.ruoyi.common.core.utils.PhoneNumberValidator;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.lvlian.domain.BusinessUserInfo;
import com.ruoyi.lvlian.service.IBusinessUserInfoService;
import com.ruoyi.lvlian.vo.BusinessUserInfoVo;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.api.model.SysDeptVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 业务人员跟车方Controller
 *
 * @author ruoyi
 * @date 2024-06-06
 */
@RestController
@RequestMapping("/businessUserInfo")
@Api(value = "业务人员跟车方", tags = "业务人员跟车方Controller")
public class BusinessUserInfoController extends BaseController {
    @Autowired
    private IBusinessUserInfoService businessUserInfoService;


    private String validateInData(BusinessUserInfo businessUserInfo) {

        if (StringUtils.isEmpty(businessUserInfo.getName())  ) {
            return "业务人员姓名不能为空";
        }
        if (StringUtils.isEmpty(businessUserInfo.getPhone()) || !PhoneNumberValidator.isValidPhoneNumber(businessUserInfo.getPhone())) {
            return "手机号码不能为空或者不对";
        }


        return "success";
    }



    @ApiOperation("通过手机号码查询业务人员信息(我的板块使用)")
    @GetMapping("/getBusinessUserByPhone")
    public AjaxResult getBusinessUserByPhone(@RequestParam @NotBlank(message = "手机号码不能为空") String phone) {
        boolean validPhoneNumber = StringUtils.isValidPhoneNumber(phone);
        if (validPhoneNumber) {
            BusinessUserInfoVo vo = businessUserInfoService.getBusinessUserByPhone(phone);
            return success(vo);
        } else {
            return error("号码不对");
        }
    }

    @ApiOperation("通过手机号码查询业务人员信息(首页切换单位使用)")
    @GetMapping("/queryDeptListByUserPhone")
    public AjaxResult queryDeptListByUserPhone(@RequestParam @NotBlank(message = "手机号码不能为空") String phone) {
        boolean validPhoneNumber = StringUtils.isValidPhoneNumber(phone);
        if (validPhoneNumber) {
            List<SysDeptVo> vo = businessUserInfoService.queryDeptListByUserPhone(phone);
            return success(vo);
        } else {
            return error("号码不对");
        }
    }


    @ApiOperation("获取所有的再生资源公司)")
    @GetMapping("/queryDeptList")
    public AjaxResult queryDeptList() {
        List<SysDeptVo> vo = businessUserInfoService.queryDeptListByUserPhone(null);
        return success(vo);
    }


    @ApiOperation("为业务人也设置默认公司")
    @PostMapping("/updateDeptIdForUser")
    public AjaxResult updateDeptIdForUser(Long userId, Long deptId) {
        return toAjax(businessUserInfoService.updateDeptIdForUser(userId, deptId));
    }


    @GetMapping("/queryDictlist")
    public TableDataInfo queryDictlist(SysDictData dictData) {
        startPage();
        List<SysDictData> list = businessUserInfoService.queryDictlist(dictData);
        return getDataTable(list);
    }

}
